package com.cxd.performance.exception;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.cxd.performance.enums.BaseCodeEnum;
import com.cxd.performance.service.ICommonBusinessException;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

/**
 * Author: Calvin
 * Date: 2019-08-30 8:40
 * Description: 业务异常处理公共类
 */
@Slf4j
@Data
public class BusinessException extends  RuntimeException{

    public String code;

    public Object[] args;

    public BusinessException() {

    }

    public BusinessException(ICommonBusinessException commonBusinessException) {
        this.code = commonBusinessException.getCode();
    }

    public BusinessException(ICommonBusinessException commonBusinessException, Object[]args) {
        this.code = commonBusinessException.getCode();
        this.args = args;
    }

    public BusinessException(String code) {
        this.code = code;
    }

    public BusinessException(String code, String msg) {
        super(msg);
        this.code = code;
    }

    public BusinessException(Exception e) {
        log.warn(e.getMessage(), e);
        this.code = BaseCodeEnum.FAILED.getCode();
    }

    public static BusinessException fail(String msg) {
        return new BusinessException(BaseCodeEnum.FAILED.getCode(), msg);
    }

    public static BusinessException fail(ICommonBusinessException businessException) {
        return new BusinessException(businessException);
    }

    public String toString() {
        return JSON.toJSONStringWithDateFormat(this, "yyyy-MM-dd HH:mm:ss", new SerializerFeature[0]);
    }

}
